from bs4 import BeautifulSoup
from pymongo import MongoClient

# 假设 HTML 内容存储在 html_content 变量中
html_content = '''
<div class="_j_commentlist">                                            
<a class="name" href="/u/48590079.html" target="_blank">一诺</a>                                                                                
<span class="s-star s-star5"></span>                                        
<p class="rev-txt">到凤凰岭进入玻璃栈道还需再买门票10元一张没有优惠。栈道有400米呈马蹄形，原来在网上看到许多视频走玻璃栈道各种造型都有，爬的，要人拉的，紧靠山边的.......我还在想我会不会害怕呢，上了栈道居然没有半点恐惧感，非常的享受，居高临下看万千景色，还有可惜这个栈道有点短，不过这里正在修建第二条玻璃栈道还有缆车，以后来可能就不要再爬山了，不过又要多花钱。</p>                                                                                                        
<div class="rev-img">                                                                                            
<a><img src="https://p1-q.mafengwo.net/s12/M00/B8/C1/wKgED1u1s3WAapZvAA_t3THmI1s38.jpeg?imageMogr2%2Fthumbnail%2F%21200x120r%2Fgravity%2FCenter%2Fcrop%2F%21200x120%2Fquality%2F100" width="200" height="120"></a>                                                                                                                            
<a><img src="https://p1-q.mafengwo.net/s12/M00/B8/C3/wKgED1u1s3eAKXNjAA6DTNwbvRI75.jpeg?imageMogr2%2Fthumbnail%2F%21200x120r%2Fgravity%2FCenter%2Fcrop%2F%21200x120%2Fquality%2F100" width="200" height="120"></a>                                                                                                                            
<a><img src="https://p1-q.mafengwo.net/s12/M00/03/69/wKgED1vrug-ARoC6AB0-oLAko6M16.jpeg?imageMogr2%2Fthumbnail%2F%21200x120r%2Fgravity%2FCenter%2Fcrop%2F%21200x120%2Fquality%2F100" width="200" height="120"></a>                                                                                                                            
<a><img src="https://p1-q.mafengwo.net/s12/M00/03/73/wKgED1vruhiALLUpABIiRHtZ5Kc80.jpeg?imageMogr2%2Fthumbnail%2F%21200x120r%2Fgravity%2FCenter%2Fcrop%2F%21200x120%2Fquality%2F100" width="200" height="120"></a>                                                        
</div>                                        
<div class="info clearfix">                                                
<a class="btn-comment _j_comment" title="添加评论">评论</a>                        
<span class="time">2018-11-16 15:25:29</span>                                                    
<span class="from">                                
此条点评来自游记<a href="/i/11194758.html" target="_blank">《走过山西，回眸一笑便是千年。（晋北，晋西...》</a>                            
</span>                                            
</div>
                                        <div class="comment add-reply hide">                        
<ul class="more_reply_box comment_list">                                                    
</ul>
                        <div class="add-comment hide reply-form">                            
<textarea class="comment_reply" data-comment_id="161988456" data-comment_username="一诺" data-poi_id="10187" data-poi_name="凤凰岭" data-parent_id="" data-parent_uid="" data-parent_username="" style="overflow: hidden; color: rgb(204, 204, 204);"></textarea>                            
<a class="btn btn_submit_reply">回复</a>                        
</div>                    
</div>                
</li>                                            
<li class="rev-item comment-item clearfix">                    
<div class="user"><a class="avatar" href="/u/70152794.html" target="_blank"><img src="https://p1-q.mafengwo.net/s19/M00/9B/38/Cpgwc2RUYRsb_wejAACjAm2N2p0.png?imageMogr2%2Fthumbnail%2F%2148x48r%2Fgravity%2FCenter%2Fcrop%2F%2148x48%2Fquality%2F90" width="48" height="48"></a><span class="level">LV.12</span></div>                                            
<a class="name" href="/u/70152794.html" target="_blank">星语悠悠</a>                                                                                
<span class="s-star s-star3"></span>                                        
<p class="rev-txt">乘坐索道上去的，游览顺序是索道——凤凰岭+玻璃栈道——茱萸峰，比较好，节省体力一些<br>
凤凰岭上其实也没什么景点，玻璃栈道对于之前没看过的游客，可以去看看，比较新鲜一些</p>                                                                                                        
<div class="rev-img">
'''

# 解析 HTML 内容
soup = BeautifulSoup(html_content, 'html.parser')

# 连接 MongoDB
client = MongoClient('mongodb://39.107.248.141:27017/')
db = client['s1']  # 替换为实际的数据库名称
collection = db['comments']

# 查找所有评价项
comment_items = soup.find_all('li', class_='rev-item comment-item clearfix')

for item in comment_items:
    # 获取用户名称
    user_name = item.find('a', class_='name').text

    # 获取评价内容
    comment_text = item.find('p', class_='rev-txt').text

    # 判断是否为好评（简单规则：星级为 4 星或 5 星视为好评）
    star_element = item.find('span', class_=lambda x: x and 's-star' in x)
    star_classes = star_element.get('class')
    star_num = int([cls for cls in star_classes if 's-star' in cls and len(cls) > 6][0].replace('s-star', ''))
    is_good_review = star_num >= 4

    # 构建评价数据
    comment_data = {
        'user': user_name,
        'comment': comment_text,
        'is_good_review': is_good_review
    }
    print(comment_data)

    # 插入到 MongoDB 中
    collection.insert_one(comment_data)

print("评价数据已成功存储到 MongoDB 中。")